ReceiveCallback.constructor   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 8.8076
c 0
b 0
f 0
cc 1
nc 1
nop 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A options.js ➔ filterData 0 5 1
A options.js ➔ update 0 7 1
A options.js ➔ getOrgData 0 3 1
A options.js ➔ init 0 8 1
A options.js ➔ setOrgData 0 5 2
A options.js ➔ setHiddenData 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * @file JS helpers for handling options in ACP.
3
 *
4
 * @author rugk
5
 * @copyright Copyright (c) 2016 rugk
6
 * @license MIT
7
 */
8
9
jQuery(document).ready(function() {
10
    'use strict';
11
12
    ReceiveCallback.init();
13
});
14
15
/**
16
 * ReceiveCallback - Handling the option threema_gateway_receivecallback.
17
 *
18
 * @return {object} Methods: update
19
 */
20
var ReceiveCallback = (function () {
21
    'use strict';
22
    var me = {};
23
    var $inputElem;
24
    var $hiddenElem;
25
26
    /**
27
     * getOrgData - Returns the input data of the field the user can see.
28
     *
29
     * @private
30
     * @return {string}
31
     */
32
    function getOrgData() {
33
        return $inputElem.text();
34
    }
35
36
    /**
37
     * setOrgData - Sets the value of the input field the user can see.
38
     *
39
     * @private
40
     * @param  {string} data The data to set.
41
     * @return {string}
42
     */
43
    function setOrgData(data) {
44
        if ($inputElem.text() !== data) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if $inputElem.text() !== data is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
45
            return $inputElem.text(data);
46
        }
47
    }
48
49
    /**
50
     * setHiddenData - Change data of the hidden input.
51
     *
52
     * @private
53
     * @param  {string} data The data to set.
54
     * @return {string}
55
     */
56
    function setHiddenData(data) {
57
        return $hiddenElem.val(data);
58
    }
59
60
    /**
61
     * filterData - Filter the input data.
62
     *
63
     * @private
64
     * @param  {string} data The data to filter.
65
     * @return {string}
66
     */
67
    function filterData(data) {
68
        // remove all bad characters
69
        // https://regex101.com/r/g4Dkb7/1
70
        return data.replace(/[^\w_-]+/ig, '');
71
    }
72
73
    /**
74
     * init - Initialize handler.
75
     *
76
     */
77
    me.init = function init() {
78
        // set variables
79
        $inputElem = $('.threemagw_receivecallback_input');
80
        $hiddenElem = $('.threemagw_receivecallback_hiddeninput');
81
82
        // register input trigger/event
83
        $inputElem.on('input', me.update);
84
    };
85
86
    /**
87
     * update - Update the output field (and, if necessary, also the output
88
     * field)
89
     *
90
     */
91
    me.update = function update() {
92
        var data;
93
94
        data = filterData(getOrgData());
95
        setOrgData(data);
96
        setHiddenData(data);
97
    };
98
99
    return me;
100
})();
101